<script>
	import {
		getCityName,getDefaultHouse,getUserInfo
	} from "./utils/getRequest.js"
	
	export default {
		onLaunch: function(p) {
			// console.log(p,'初始化的时候判断是否进来了');
			// if(p.query.residentialquarters_id){
			// 	console.log(p,'初始化是否进来了');
			// 	let residentialquarters_id = p.query.residentialquarters_id;
			// 	let residentialquarters_name = p.query.residentialquarters_name;
			// 	if(!this.$store.getters.token){
			// 		console.log(p,'初始化是否进来了4');
			// 		uni.reLaunch({
			// 			url: '/pagesLogin/index?residentialquarters_id='+residentialquarters_id+'&residentialquarters_name='+residentialquarters_name
			// 		})
					
			// 	}
			// }else{
			// 	console.log(p,'初始化是否进来了2');
			// 	if(!this.$store.getters.token){
			// 		console.log(p,'初始化是否进来了3');
			// 		uni.reLaunch({
			// 			url: '/pagesLogin/index'
			// 		})
					
			// 	}
			// }
			// console.log(this.$store.getters.token,'是否登录')
			// if(!this.$store.getters.token){
			// 	console.log('是否登录3')
			// 	uni.reLaunch({
			// 		url: '/pagesLogin/index'
			// 	})
			// 	// getUserInfo(2,null)
			// 	// getUserInfo().then((res)=>{
			// 	// 	console.log(res,'是否登录2')
			// 	// }
			// 	// )
				
			// }
			//#ifdef APP-PLUS
			// uni-app客户端获取push客户端标记
			// uni.getPushClientId({
			// 	success: (res) => {
			// 		let push_clientid = res.cid
			// 		console.log('客户端推送标识:',push_clientid)
			// 		uni.setStorageSync('push_clientid', push_clientid);
			// 	},
			// 	fail(err) {
			// 		console.log(err)
			// 	}
			// })
			
			// console.log('推送消息监听')
			// uni.onPushMessage((res) => {
			// 	console.log("收到推送消息：",res) //监听推送消息
			// })
			//#endif
					
			// #ifndef H5
			uni.getSystemInfo({
			     success:  (res)=> {
					 let model = ["X", "XR", "XS", "11", "12", "13", "14", "15"];
						 model.forEach((item) => {
						   if (
							 res.model.indexOf(item) != -1 &&
							 res.model.indexOf("iPhone") != -1
						   ) {
							 this.$store.commit('setIsIOS',true)
						   }
						 });
			     },
				
				
			});
			// #endif
			setTimeout(()=> {
				var pages = getCurrentPages()
				var page =  pages   && pages.length > 0 ? pages[pages.length -1] : null
				if(page && page.options &&Object.keys(page.options).length > 0 ){
					var row = Object.keys(page.options).find(c=>c == 'invitation_code')
					if(row && row != 'null' && row != 'undefined' ) this.$store.commit('setInvitationCode', page.options[row])
				}
			},500)
			// setTimeout(()=> {
				// #ifdef APP-PLUS || MP-WEIXIN
				// if(this.$store.state.userInfo && !this.$IM){
				// 	this.$store.commit('IMINIT')
				// }
				// #endif
				if( this.$store.state.userInfo){
					getDefaultHouse().then((res)=>{
						this.$store.commit('setDefaultHouse', Object.prototype.toString.call(res) === '[object Object]' ? res : null)
					})
				}
			// }, 300);
		},
		onShow: function() {
			// if(!this.$store.getters.token){
			// 	// getUserInfo().then((res)=>{
			// 	// 	console.log(res,'是否登录2')
			// 	// })
			// 	uni.reLaunch({
			// 		url: '/pagesLogin/index'
			// 	})
				
			// }
			
			uni.getSystemInfo({
				success: (e) => {
					if (e && e.statusBarHeight) this.$store.commit('setStatusBarHeight', e.statusBarHeight)
				}
			})
			//#ifdef APP-PLUS
			this.$store.commit('setDuanType', 1)
			//#endif
			//#ifdef MP-WEIXIN
			this.$store.commit('setDuanType', 2)
			//#endif
			
			//#ifdef MP-WEIXIN
			var c = wx.getMenuButtonBoundingClientRect()
			if (c) {
				// bottom: 56	// height: 32  // left: 278  // right: 365   // top: 24 // width: 87
				this.$store.commit('setClientRect', c)
			}
			//#endif
			//#ifdef APP-PLUS ||  MP-WEIXIN
			if (!this.$store.state.defaultHouse && this.$store.state.isOpenSetting != 1 && new Date().getTime() - this.$store.state.isOpenSettingshow > 30000){ // 0 从未 1 正在 2 结束
				uni.getLocation({
					type: 'wgs84',
					success: res => {
						getCityName
							({longitude: res.longitude,latitude: res.latitude})
							.then(
								res2 => {
									if(this.$store.state.isOpenSetting == 1)uni.showToast({
										title: '授权成功',icon:'none'
									})
									res.cityInfo =res2
									res.cityId =res2.id
									res.cityName =res2.name
									this.$store.commit('setCurrentArea',res)
									this.$store.commit('setIsOpenSetting',2)
									
								})
							.catch(() => {})
					},
					fail: err => {
					// #ifdef  MP-WEIXIN 
						uni.getSetting({
								success: res => {
									var statu = res.authSetting;
									if (!res.authSetting['scope.userLocation']) {
										wx.showModal({
											title: '是否授权当前位置',
											content: '需要获取您的地理位置，仅在获取商品、店铺、等信息时使用，不做其他用途。',
											fail:()=>{
												this.$store.commit('setIsOpenSetting',2)
												this.$store.commit('setIsOpenSettingshow',new Date().getTime())
											},
											success: (tip) => {
												if (tip.confirm) {
													this.$store.commit('setIsOpenSetting', 1)
													wx.openSetting({
														success: (data) => {
															if (data.authSetting["scope.userLocation"] === true) {
																uni.getLocation({
																	type: 'wgs84',
																	success: res => {
																		getCityName
																			({longitude: res.longitude,latitude: res.latitude})
																			.then(
																				res2 => {
																					uni.showToast({
																						title: '授权成功',icon:'none'
																					})
																					res.cityInfo =res2
																					res.cityId =res2.id
																					res.cityName =res2.name
																					this.$store.commit('setCurrentArea',res)
																					this.$store.commit('setIsOpenSetting',2)
																					this.$store.commit('setIsOpenSettingshow',new Date().getTime())
																				})
																			.catch(() => {})
																	},
																	fail: err => {
																		this.$store.commit('setIsOpenSetting',2)
																		this.$store.commit('setIsOpenSettingshow',new Date().getTime())
																	}
																})
															} else {
																wx.showToast({
																	title: '授权失败',icon:'none',duration: 1000
																})
																this.$store.commit('setIsOpenSetting',2)
																this.$store.commit('setIsOpenSettingshow',new Date().getTime())
															}
														}
													})
												}else{
													this.$store.commit('setIsOpenSetting',2)
													this.$store.commit('setIsOpenSettingshow',new Date().getTime())
												}
											}
										})
									}
								},
								fail: function(res) {
									this.$store.commit('setIsOpenSetting',2)
									this.$store.commit('setIsOpenSettingshow',new Date().getTime())
									wx.showToast({
										title: '授权失败',
										icon: 'none',
										duration: 1000
									})
								}
							})
						
					//#endif
					}
					
				})
			}
			//#endif
			},
			onHide: function() {
				console.log('App Hide')
			}
		}
</script>

<style>
	/*每个页面公共css */
/*  #ifdef  H5  */
/* body {
  max-width: 828rpx;  //最大宽度自己可以调整
  margin: auto !important;
} */
/*  #endif  */
</style>
